import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/style/app_style.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/models/home/product_detail_model.dart';
import 'package:laos_mall/pages/main/catergory/product_detail/logic.dart';

class SkuItemWidget extends StatefulWidget {
   SkuItemWidget({super.key,this.selIndex = 0,this.isSkuPage = true,this.model});
   int selIndex;
   final bool isSkuPage;
   final ProductDetailModel? model;
   
  @override
  State<SkuItemWidget> createState() => _SkuItemWidgetState();
}

class _SkuItemWidgetState extends State<SkuItemWidget> {
  @override
  Widget build(BuildContext context) {
    final logic = Get.find<ProductDetailLogic>();
    
    return  ListView.builder(
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (_, index) {
        var title = widget.model?.attr_list?.spe[index].attr_name ?? "Color";
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(title ?? "",
                style: AppStyle.textStyle),
            const SizedBox(
              height: 12,
            ),
            GridView.builder(physics: NeverScrollableScrollPhysics(),padding: EdgeInsets.zero,gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 70.w,childAspectRatio: 70/26,crossAxisSpacing: 18.w,mainAxisSpacing: 14.h), itemBuilder: (_,subIndex){
              var skuName = widget.model?.attr_list?.spe[index].attr_list[subIndex].attr_value;

              var findSku = widget.model?.sku_list?.firstWhereOrNull((element) => element.sku_id == logic.skuId);
              var skuItem = findSku?.sku_data.firstWhereOrNull((element) => title == element.name);
              var isSel = false;
              if(skuItem?.value == skuName){
                isSel = true;
              }else{
                isSel = false;
              }
              return  Text(
                skuName ?? "",
                style: AppStyle.subtitleStyle.copyWith(color: isSel ? AppColor.white : AppColor.redColor),
              )
                  .addContainer(
                color:
                isSel ?
                AppColor.primaryColor
                    : AppColor.transparent,
                padding: const EdgeInsets.symmetric(
                    horizontal: 0, vertical: 0),
                alignment: Alignment.center
              )
                  .addGestureTap(() {
                // widget.selIndex = subIndex;
                  logic.jumpToSku();
              });
            },itemCount: widget.model?.attr_list?.spe[index].attr_list.length,shrinkWrap: true,).addContainer(color: Colors.transparent),
          ],
        ).addPadding(EdgeInsets.only(bottom: 12.h));
      },
      shrinkWrap: true,
      itemCount: widget.model?.attr_list?.spe.length ?? 0,
      padding: EdgeInsets.zero,
    ).addPadding( EdgeInsets.symmetric(horizontal: widget.isSkuPage ? 20.w : 0, vertical: widget.isSkuPage ? 12.h : 0));
  }
}


